﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FAData
{
    public class FAAFlightData
    {
        // Finds the airport departure delay from the FAA City Pair Analysis Report.
        public int DepartureDelay(string departureCode, string arrivalCode)
        {

            DataClasses1DataContext db = new DataClasses1DataContext();

            var departureDelayQuery = (from faa in db.FAACityPairAnalysis
                                       where faa.DepartureCode == departureCode
                                       where faa.ArrivalCode == arrivalCode
                                       select new { faa.AirportDepartureDelay }).SingleOrDefault();

            int departureDelay = (int)departureDelayQuery.AirportDepartureDelay;

            db.Dispose();

            return departureDelay;

        }

        // Finds the airpots arrival delay from the FAA City Pair Analysis Report.
        public int ArrivalDelay(string departureCode, string arrivalCode)
        {

            DataClasses1DataContext db = new DataClasses1DataContext();

            var arrivalDelayQuery = (from faa in db.FAACityPairAnalysis
                                     where faa.DepartureCode == departureCode
                                     where faa.ArrivalCode == arrivalCode
                                     select new { faa.GateArrivalDelay }).SingleOrDefault();

            int arrivalDelay = (int)arrivalDelayQuery.GateArrivalDelay;

            db.Dispose();

            return arrivalDelay;

        }

        // Finds the percent of flights arriving late for more than 15 minutes for an airport
        // due to "extreme" weather as the only factor.
        public double ExtremeWeatherOnlyFactorYearlyPercentDelay(string airportCode)
        {
            DataClasses1DataContext db = new DataClasses1DataContext();

            var percentOfOnTimeFlights = (from faa in db.FAAAirportDelays
                                          where faa.AirportCode == airportCode
                                          select new { faa.WeatherFactorPercent }).SingleOrDefault();


            double percentWeatherDelay = 100 - (double)percentOfOnTimeFlights.WeatherFactorPercent;



            db.Dispose();

            return percentWeatherDelay;

        }


        // Finds the percent of flights arriving late for more than 15 minutes for an airport
        // due to weather as the only factor as stated by the National Aviation System (NAS).
        // Note that the NAS 'weather' delay is 62.23% of the delays; other factors are 'Volume (31.2%),
        // 'Closed Runway'  and 'Other'.
        public double NasWeatherOnlyFactorYearlyPercentDelay(string airportCode)
        {
            DataClasses1DataContext db = new DataClasses1DataContext();

            var percentaNasOnTimeFlights = (from faa in db.FAAAirportDelays
                                            where faa.AirportCode == airportCode
                                            select new { faa.NasCausePercent}).SingleOrDefault();
            
            double percentNasWeatherDelay = (100 - (double) percentaNasOnTimeFlights.NasCausePercent) * .6223;

            db.Dispose();

            return percentNasWeatherDelay;
        }


        // Finds the average minutes delayted due to weather for an airline in specific airports
        public int AirlineWeatherDelayMinutes (string airlineCode, string airportCode)
        {
            DataClasses1DataContext db = new DataClasses1DataContext();

            var airlineWeatherDelayMinutes =  (from faa in db.DelaysByAirlines
                                       where faa.airport == airportCode
                                       where faa.carrier == airlineCode
                                       select new { faa.weather_delay, faa.nas_delay, faa.arr_delay }).FirstOrDefault();  // note: database may have these values repeated

            //var airlineNasDelayMinutes = (from faa in db.DelaysByAirlines
            //                          where faa.airport == airportCode
            //                          where faa.carrier == airlineCode
            //                              select new { faa.nas_delay }).FirstOrDefault();                       


            int delayMinutes = (int)(airlineWeatherDelayMinutes.weather_delay + airlineWeatherDelayMinutes.nas_delay * 0.6223/airlineWeatherDelayMinutes.arr_delay ) ;

            db.Dispose();

            return delayMinutes;
        }



    }
    
}
